struct ListNode* deleteDuplicates(struct ListNode* head)
{
    // write code here
    if (head == NULL || head->next == NULL)
        return head;

    struct ListNode* tail = NULL;
    struct ListNode* newhead = NULL;

    struct ListNode* slow = head;
    struct ListNode* fast = slow->next;

    int flag = 1;
    while (fast)
    {
        if (slow->val == fast->val)
        {
            fast = fast->next;
            flag = 0;
        }
        else
        {

            if (flag == 0)
            {
                slow = fast;
                fast = fast->next;
                flag = 1;
                continue;
            }

            if (newhead == NULL)
            {
                tail = newhead = slow;
            }
            else
            {
                tail->next = slow;
                tail = slow;
            }
            slow = fast;
            fast = fast->next;
        }
    }

    if (flag == 1)
    {
        if (tail)
            tail->next = slow;
        else
            newhead = slow;
    }
    else
    {
        if (tail)
            tail->next = NULL;
    }

    return newhead;
}